Object.defineProperty(exports, "__esModule", {
    value: !0
});

var t = getApp(), e = require("../../utils/api"), i = require("../../utils/config"), a = require("../../utils/request"), n = require("../../utils/storage"), o = require("../../utils/map-convert");

Page({
    data: {
        themeColor: t.globalData.themeColor,
        itemTitle: "营业日",
        optionBrand: [ {
            text: "品牌",
            value: ""
        }, {
            text: "瑞慈体检",
            value: "10"
        }, {
            text: "幸元会体检",
            value: "20"
        } ],
        workDaysOptions: [ {
            text: "周一",
            value: "1"
        }, {
            text: "周二",
            value: "2"
        }, {
            text: "周三",
            value: "3"
        }, {
            text: "周四",
            value: "4"
        }, {
            text: "周五",
            value: "5"
        }, {
            text: "周六",
            value: "6"
        }, {
            text: "周日",
            value: "0"
        } ],
        city: "",
        cityCode: "",
        latitude: "",
        longitude: "",
        rgcData: {},
        showPopup: !1,
        allInstitution: [],
        institution: [],
        institutionNameSearch: "",
        checkedWorkDays: [],
        checkedBrand: "",
        times: 0,
        showDistance: !0,
        loading: !1,
        privacyContractName: "",
        showPrivacy: !1
    },
    onConfirm: function() {
        this.selectComponent("#item").toggle(), this.getFilterInstitutionData();
    },
    onLoad: function() {
        var t = this;
        wx.getPrivacySetting({
            success: function(e) {
                e.needAuthorization ? t.setData({
                    privacyContractName: e.privacyContractName,
                    showPrivacy: !0
                }) : t.getLoc();
            }
        });
    },
    onInstitutionNameSearch: function(t) {
        this.setData({
            institutionNameSearch: t.detail
        }), this.getFilterInstitutionData();
    },
    onInstitutionNameCancel: function() {
        this.setData({
            institutionNameSearch: ""
        }), this.getFilterInstitutionData();
    },
    checkboxChange: function(t) {
        var e = [];
        t.detail.forEach(function(t) {
            t.checked && e.push(t.text);
        }), this.setData({
            checkedWorkDays: e
        });
    },
    brandChange: function(t) {
        this.setData({
            checkedBrand: t.detail
        }), this.getFilterInstitutionData();
    },
    getFilterInstitutionData: function() {
        var t = this, e = this.data.allInstitution, i = this.data.institutionNameSearch.replace(" ", "");
        if ("" != i && (e = e.filter(function(t) {
            return t.name.indexOf(i) > -1;
        })), "" != this.data.checkedBrand && (e = this.data.allInstitution.filter(function(e) {
            return e.brandName == t.data.checkedBrand;
        })), this.data.checkedWorkDays.length > 0) {
            var a = [];
            e.forEach(function(e) {
                var i = !0;
                t.data.checkedWorkDays.forEach(function(t) {
                    e.workday.indexOf(t) > -1 || (i = !1);
                }), i && a.push(e);
            }), e = a;
        }
        this.setData({
            institution: e
        });
    },
    getLoc: function() {
        var t = this, e = n.getStorageSync(n.storageKey.locationInfo);
        // console.log(11);return
        if (null != e && null != e && "" != e && e.expireTimestamp > new Date().getTime()) {
            this.setData({
                latitude: e.latitude,
                longitude: e.longitude
            });
            var i = n.getStorageSync(n.storageKey.cityInfo);
            null != i && null != i && "" != i ? (this.setData({
                city: i.city,
                cityCode: i.cityCode
            }), t.selectWcaInstitutionByCityCode()) : t.selectCityByCoordinates();
        } else wx.getLocation({
            type: "gcj02",
            success: function(e) {
                var i = o.qqMapToBMap(e.longitude, e.latitude);
                t.setData({
                    latitude: i[1],
                    longitude: i[0]
                }), n.setStorageSync(n.storageKey.locationInfo, {
                    expireTimestamp: new Date().getTime() + 6e5,
                    latitude: i[1].toString(),
                    longitude: i[0].toString()
                }), t.selectCityByCoordinates();
            },
            fail: function(t) {
                console.log("微信获取位置信息失败：" + t.errMsg), t.errMsg.indexOf("频繁") > -1 ? console.log("请勿频繁定位") : wx.showModal({
                    title: "提示",
                    content: "如果您想要显示推荐机构，可点击小程序右上角“···”，设置中允许使用位置信息，并重新进入小程序",
                    showCancel: !1
                });
            }
        });
    },
    openPrivacyContract: function() {
        wx.openPrivacyContract({
            fail: function() {
                wx.showToast({
                    title: "遇到错误",
                    icon: "error"
                });
            }
        });
    },
    exitMiniProgram: function() {
        this.setData({
            showPrivacy: !1
        });
    },
    handleAgreePrivacyAuthorization: function() {
        this.setData({
            showPrivacy: !1
        }), this.getLoc();
    },
    selectCityByCoordinates: function() {
        var t = this, i = e.apiRoute.selectCityByCoordinates + "?latitude=" + this.data.latitude + "&longitude=" + this.data.longitude;
        a.request(a.HttpMethod.GET, i, {}).then(function(e) {
            if (e.success) {
                var i = e.data.filter(function(t) {
                    return "1" == t.deep;
                });
                null != i && null != i && i.length > 0 && (t.setData({
                    city: i[0].name,
                    cityCode: i[0].id + "00"
                }), n.setStorageSync(n.storageKey.cityInfo, {
                    city: i[0].name,
                    cityCode: i[0].id + "00"
                }));
            }
            t.selectWcaInstitutionByCityCode();
        });
    },
    showCitys: function() {
        this.setData({
            showPopup: !0
        });
    },
    onClose: function() {
        this.setData({
            showPopup: !1
        });
    },
    cityChange: function(t) {
        this.setData({
            showPopup: !1,
            city: t.detail.name,
            cityCode: t.detail.code
        }), this.selectWcaInstitutionByCityCode();
    },
    selectWcaInstitutionByCityCode: function() {
        var t = this;
        wx.showLoading({
            title: "正在加载中...",
            mask: !0
        }), this.setData({
            loading: !0
        });
        var n = this.data.latitude, o = this.data.longitude;
        "" == this.data.latitude && (n = "0"), "" == o && (o = "0");
        var s = e.apiRoute.selectWcaInstitutionByCityCode + "?cityCode=" + this.data.cityCode + "&latitude=" + n + "&longitude=" + o;
        a.request(a.HttpMethod.GET, s, {}).then(function(e) {
            "20000" == e.code ? (e.data.forEach(function(t) {
                null != t.imageList ? t.jgzp = i.institutionImageUrl + t.imageList[0] : t.jgzp = i.defaultInstitutionImageURL;
            }), t.setData({
                institution: e.data,
                allInstitution: e.data
            }), t.getFilterInstitutionData()) : t.setData({
                institution: [],
                allInstitution: []
            }), t.setData({
                loading: !1
            });
        }), wx.hideLoading();
    },
    showAll: function() {
        this.setData({
            cityCode: "",
            city: ""
        });
        var t = n.getStorageSync(n.storageKey.locationInfo);
        null != t && null != t && "" != t && n.setStorageSync(n.storageKey.locationInfo, {
            cityCode: "",
            city: "",
            latitude: t.latitude,
            longitude: t.longitude
        }), this.selectWcaInstitutionByCityCode();
    },
    onShareAppMessage: function() {
        return {
            title: "瑞慈体检预约 - 分院",
            path: "/pages/institution/institution"
        };
    },
    onShareTimeline: function() {
        return {
            title: "瑞慈体检预约 - 分院",
            path: "/pages/institution/institution"
        };
    },
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
    }
});